package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.LeaveDAO;
import com.pentofab.payrollmanagement.dao.SalaryDAO;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.dao.model.EsiPfBonus;
import com.pentofab.payrollmanagement.dao.model.Holiday;
import com.pentofab.payrollmanagement.dao.model.HolidaysDetail;
import com.pentofab.payrollmanagement.dao.model.Leave;
import com.pentofab.payrollmanagement.dao.model.LeaveDetail;
import com.pentofab.payrollmanagement.dao.model.PTSlab;
import com.pentofab.payrollmanagement.dao.model.PTSlabDetail;
import com.pentofab.payrollmanagement.dao.model.UserAccounts;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.ui.control.CustomTextField;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;

public class ESIPFBonusController extends AbstractUIController implements
	Initializable, FormValidator{
	

    @FXML
    private VBox root;
    @FXML
    private CustomTextField esi;
    @FXML
    private CustomTextField pf;
    @FXML
    private CustomTextField bonus;
    
    SalaryDAO salaryDAO =PayrollApplicationFactory.getSalaryDAO();
	
	private ArrayList<String> fromMonthDaysList = new ArrayList<String>();

	public void goBack(ActionEvent event) {

		if (getParent() != null && getParent().length() > 0) {
			PayrollApplicationFactory.getMainController().showScreen(
					getParent(), false);
		}
	
	}
	public void save() {
		
		EsiPfBonus esiPfBonus = new EsiPfBonus();
		try {
			
			String validationMsg = getValidationMessage();
			if(!validationMsg.equals("")){
				showError(validationMsg);
				return;
			}
			esiPfBonus.setEsi(Double.parseDouble(esi.getText()));
			esiPfBonus.setPf(Double.parseDouble(pf.getText()));
			esiPfBonus.setBonus(Double.parseDouble(bonus.getText()));
			
			salaryDAO.setEsiPfBonus(esiPfBonus);
		} catch (BusinessException e) {
			e.printStackTrace();
			showError(e.getMessage());
		}
	
	}
	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return "ESI PF Bonus";
	}
	
	@Override
	public void refreshUI(Object... args) {
		
		EsiPfBonus esiPfBonus;
		try {
			esiPfBonus = salaryDAO.getEsiPfBonus();
		
			esi.setText(String.valueOf(esiPfBonus.getEsi()));
			pf.setText(String.valueOf(esiPfBonus.getPf()));
			bonus.setText(String.valueOf(esiPfBonus.getBonus()));
		
		} catch (BusinessException e) {
			e.printStackTrace();
			showError(e.getMessage());
		}
	}
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		
	}

	
	
	@Override
	public String getValidationMessage() {
		try{
			Double.parseDouble(esi.getText());
		}catch(Exception e){
		
			return "Please enter either 0.0 or ESI % in input field";
		}
		
		try{
			Double.parseDouble(pf.getText());
		}catch(Exception e){
			
			return "Please enter either 0.0 or PF  % in input field";
		}
		
		try{
			Double.parseDouble(bonus.getText());
		}catch(Exception e){
			
			return "Please enter either 0.0 or Bonus  % in input field";
		}
		
		return "";
	}

	@Override
	public boolean isValid() {
		return false;
	}
}
